<?php
/*
CREATE TABLE IF NOT EXISTS `article` (
  `itemid` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `content` text NOT NULL,
  PRIMARY KEY (`itemid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
*/
//https://www.cnblogs.com/mracale/p/10774047.html
//https://www.cnblogs.com/lishuaige/articles/5545921.html
//https://www.cnblogs.com/fuhai0815/p/9792520.html
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header("Access-Control-Allow-Credentials: true");





header("content-type:application/json;charset=utf-8");
error_reporting(E_ALL);
ini_set('display_errors',1);
set_time_limit(0);
date_default_timezone_set('Asia/Shanghai');

function todata($code=0,$msg='success',$data=[]){
	return ['code'=>$code,'msg'=>$msg,'data'=>$data];
}

$dsn = 'mysql:dbname=mp_curd;host=127.0.0.1';
$user = 'root';
$password = 'root';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
	exit;
}

class Server{
	public function __construct(){
		
	}
	public function index(){
		return 'index';
	}
	public function error_404(){
		return 'error_404';
	}
	public function login($data){
		global $dbh;
		extract($data);
		$appid = 'wx9ecd6f55cb302ad8';
		$appsecret = '58d32045705fd1ec2796f771119930a5';
		isset($code) or $code='';

		$data=file_get_contents("https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$appsecret}&js_code={$code}&grant_type=authorization_code");
		
		$json=json_decode($data,true);
		
		$openid=$json['openid'];
		
		$sth=$dbh->prepare("select * from user where openid=:openid");
		$sth->execute(['openid'=>$openid]);
		$result=$sth->fetch(PDO::FETCH_ASSOC);
		if($result){
			return todata(0,'',['userid'=>$result['userid']]);
		}else{
			$sth=$dbh->prepare("insert into user(username,password,openid,unionid) values (:username,:password,:openid,:unionid)");
			$r=$sth->execute(['username'=>'','password'=>'','openid'=>$openid,'unionid'=>'']);
			if($r){
				$userid=$dbh->lastInsertId();
				return todata(0,'',['userid'=>$userid]);
			}else{
				return todata(1,'登录失败');
			}
		}
		
	}
	public function add($data){
		global $dbh;
		extract($data);
		$sth=$dbh->prepare("insert into article(title,content) values (:title,:content)");
		$r=$sth->execute(['title'=>$title,'content'=>$content]);
		if($r){
			return todata();
		}else{
			return todata(1,'插入失败');
		}
	}
	public function mod($data){
		global $dbh;
		extract($data);
		isset($itemid) or $itemid=0;
		$itemid=intval($itemid);
		$sth=$dbh->prepare("update article set title=:title,content=:content where itemid=:itemid");
		$r=$sth->execute(['title'=>$title,'content'=>$content,'itemid'=>$itemid]);
		if($r){
			return todata();
		}else{
			return todata(1,'修改失败');
		}
	}
	public function lst($data){
		global $dbh;
		extract($data);
		isset($page) or $page=1;
		$page=intval($page);
		$pagesize=10;
		$offset=($page-1)*$pagesize;
		$sth=$dbh->prepare("select * from article where 1=1 order by itemid desc limit {$offset},{$pagesize}");
		$sth->execute();
		$result=$sth->fetchAll(PDO::FETCH_ASSOC);
		$sth=$dbh->prepare("select count(*) from article where 1=1");
		$sth->execute();
		$result2=$sth->fetch(PDO::FETCH_NUM);
		$total_page=ceil($result2[0]/$pagesize);
		return todata(0,'',['lst'=>$result,'total_page'=>$total_page,'sql'=>"select * from article where 1=1 order by itemid desc limit {$offset},{$pagesize}"]);
	}
	public function show($data){
		global $dbh;
		extract($data);
		isset($itemid) or $itemid=0;
		$itemid=intval($itemid);
		$sth=$dbh->prepare("select * from article where itemid=:itemid");
		$sth->execute(['itemid'=>$itemid]);
		$result=$sth->fetch(PDO::FETCH_ASSOC);
		if($result){
			return todata(0,'',$result);
		}else{
			return todata(1,'not found');
		}
	}
	public function del($data){
		global $dbh;
		extract($data);
		isset($itemid) or $itemid=0;
		$itemid=intval($itemid);
		$sth=$dbh->prepare("delete from article where itemid=:itemid");
		$r=$sth->execute(['itemid'=>$itemid]);
		if($r){
			return todata();
		}else{
			return todata(1,'删除失败');
		}
	}
}

isset($_GET['action']) or $_GET['action']='index';
$action=$_GET['action'];
$server=new Server();
if(method_exists($server,$action) and !in_array($action,['__construct','__destruct'])){
	echo json_encode($server->$action(array_merge($_GET,$_POST)));
}else{
	echo $server->error_404();
}

